package bysj.controller.basic;

import bysj.domain.Student;
import bysj.domain.StudentClass;
import bysj.domain.Teacher;
import bysj.service.StudentClassService;
import bysj.service.StudentService;
import bysj.service.TeacherService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import util.Condition;
import util.JSONUtil;
import util.Pagination;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
@Slf4j
@WebServlet("/student.ctl")
public class StudentController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
            throws ServletException, IOException {
        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        //将request body转换为JSON字串
        String req_jsonStr = JSONUtil.getJSON(request);
        //将request body解析为JSONObject对象
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        //如果请求body为空
        if (req_jsonObj == null) {
            respMessage_jsonObj.put("message","未收到请求信息！");
            response.getWriter().println(respMessage_jsonObj);
            return;
        }
        //从JSONObject对象中读取键“id”的值（Java Object对象），req_jsonObj可能是{"id":1}
        Object id_obj = req_jsonObj.get("id");
        try {
            //如果id_Obj != null, 说明请求数据有id键，是请求某个教师对象
            if (id_obj != null) {
                int id_int = Integer.parseInt(id_obj.toString());
                responseStudent(id_int, response);
            } else {
                Object teacher_obj = req_jsonObj.get("teacherId").toString();
                if(teacher_obj!=null){
                    int teacher_id = Integer.parseInt(teacher_obj.toString());
                    Teacher teacher = TeacherService.getInstance().find(teacher_id);
                    Pagination pagination = getPagination(req_jsonObj);
                    this.responseStudents(response, teacher, pagination);
                }else{
                    Pagination pagination = getPagination(req_jsonObj);
                    //从请求JSONObject对象中读取键“conditionList”对应的值(描述查询条件的JSONArray对象)
                    List<Condition> conditionList = getConditions(req_jsonObj);
                    //响应多个教师信息，其中conditionList, pagination两个对象引用为空是可能的
                    this.responseStudents(response, conditionList, pagination);
                }
                //请求中包含条件和分页信息，需要响应多个教师对象
                //读取分页信息，形成分页对象
                //从JSONObject对象中读取 “pagination” 键对应的值(JSONObject对象)
                Pagination pagination = getPagination(req_jsonObj);
                //从请求JSONObject对象中读取键“conditionList”对应的值(描述查询条件的JSONArray对象)
                List<Condition> conditionList = getConditions(req_jsonObj);
                //响应多个教师信息，其中conditionList, pagination两个对象引用为空是可能的
                this.responseStudents(response, conditionList, pagination);
            }
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            //响应respMessage_jsonObj到前端
            response.getWriter().println(respMessage_jsonObj);
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            //响应respMessage_jsonObj到前端
            response.getWriter().println(respMessage_jsonObj);
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        }
    }

    /**
     * 获得代表查询条件的List对象
     * @param req_jsonObj 请求body对应的JSONObject对象
     * @return 代表查询条件的List对象
     */
    private List<Condition> getConditions(JSONObject req_jsonObj) {
        JSONArray conditionList_jsonArray =
                req_jsonObj.getJSONArray("conditionList");
        //条件对象
        List<Condition> conditionList = null;
        //如果有“conditionList”键
        if (conditionList_jsonArray != null) {
            //将描述条件的 JSONArray 对象转换为 Java的List 对象
            conditionList = conditionList_jsonArray.toJavaList(Condition.class);
        }
        return conditionList;
    }
    /**
     * 获得代表查询条件的Pagination对象
     * @param req_jsonObj 请求body对应的JSONObject对象
     * @return 代表查询条件的Pagination对象
     */
    private Pagination getPagination(JSONObject req_jsonObj) {
        JSONObject pagination_jsonObj =
                req_jsonObj.getJSONObject("pagination");
        //分页对象
        Pagination pagination = null;
        //如果有“pagination”键
        if (pagination_jsonObj != null) {
            //将描述分页的 JSONObject 对象转换为 Java 对象
            pagination = pagination_jsonObj
                    .toJavaObject(Pagination.class);
        }
        return pagination;
    }

    //按分页信息响应符合条件的教师对象和对象总数
    private void responseStudents(HttpServletResponse response,
                                  Teacher teacher,
                                  Pagination pagination)
            throws ServletException, IOException, SQLException {
        //分页获得满足条件的所有教师
        Collection<Student> students = StudentService.getInstance()
                .findAll(teacher, pagination);
        //第二个参数可以防止循环引用
        String teachers_jsonStr = JSON.toJSONString(students,
                SerializerFeature.DisableCircularReferenceDetect);
        //创建JSON对象resp_jsonObj，以便往前端响应综合信息
        JSONObject resp_jsonObj = new JSONObject();
        resp_jsonObj.put("totalNum", pagination.getTotalNum());
        resp_jsonObj.put("data", teachers_jsonStr);
        //响应resp_jsonObj到前端
        response.getWriter().println(resp_jsonObj);
    }
    private void responseStudents(HttpServletResponse response,
                                  List<Condition>conditionList,
                                  Pagination pagination)
            throws ServletException, IOException, SQLException {
        //分页获得满足条件的所有教师
        Collection<Student> students = StudentService.getInstance()
                .findAll(conditionList, pagination);
        //第二个参数可以防止循环引用
        String teachers_jsonStr = JSON.toJSONString(students,
                SerializerFeature.DisableCircularReferenceDetect);
        //创建JSON对象resp_jsonObj，以便往前端响应综合信息
        JSONObject resp_jsonObj = new JSONObject();
        resp_jsonObj.put("totalNum", pagination.getTotalNum());
        resp_jsonObj.put("data", teachers_jsonStr);
        //响应resp_jsonObj到前端
        response.getWriter().println(resp_jsonObj);
    }

    //响应一个教师对象
    private void responseStudent(int id, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //根据id查找教师
        Student student = StudentService.getInstance().find(id);
        String teacher_jsonStr = JSON.toJSONString(student);
        //响应teacher_json到前端
        response.getWriter().println(teacher_jsonStr);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        //根据request对象，获得代表新增Teacher的JSON字串
        String req_studentToAdd_jsonStr = JSONUtil.getJSON(request);
        //将request body解析为JSONObject对象
        JSONObject req_jsonObj = JSON.parseObject(req_studentToAdd_jsonStr);
        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        try {
            int studentClass_id = Integer.parseInt(req_jsonObj.get("studentClass_id").toString());
            StudentClass studentClass = StudentClassService.getInstance().find(studentClass_id);
            int teacher_id = Integer.parseInt(req_jsonObj.get("teacher_id").toString());
            System.out.println(teacher_id);
            Teacher teacher = TeacherService.getInstance().find(teacher_id);
            System.out.println(teacher);
            Student studentToAdd = JSON.parseObject(req_studentToAdd_jsonStr,
                    Student.class);
            studentToAdd.setStudentClass(studentClass);
            studentToAdd.setSupervisor(teacher);
            System.out.println(studentToAdd);
            //在数据库表中增加Teacher对象
            StudentService.getInstance().add(studentToAdd);
            respMessage_jsonObj.put("message", "增加成功");
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            e.printStackTrace();
            //打印异常栈，方便调试（要用log4j代替）
            //log.error(e.getMessage());
        }
        //响应respMessage_jsonObj到前端
        response.getWriter().println(respMessage_jsonObj);
    }

    /**
     * DELETE, http://localhost:8080/degree.ctl?id=1, 删除id=1的学位
     * 删除一个学位对象：根据来自前端请求的id，删除数据库表中id的对应记录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将request参数转换为JSON字串
        String request_json_str = JSONUtil.getJSON(request);
        //将请求body解析为要删除的Teacher的id对应的JSONObject对象(可能是{"id":1})
        JSONObject idOfDegreeToDelete_jsonObj = JSON.parseObject(request_json_str);
        //从JSONObject对象中读取键“id”的值（Java Object对象），
        Object id_obj = idOfDegreeToDelete_jsonObj.get("id");
        int id_int = Integer.parseInt(id_obj.toString());
        System.out.println("id=" + id_int);//用于调试，要使用log4j代替
        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        try {
            //到数据库表中删除对应的教师
            StudentService.getInstance().delete(id_int);
            respMessage_jsonObj.put("message", "删除成功");
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        }
        //响应respMessage_jsonObj到前端
        response.getWriter().println(respMessage_jsonObj);
    }


    /**
     * PUT, http://localhost:8080/degree.ctl, 修改学位
     *
     * 修改一个学位对象：将来自前端请求的JSON对象，更新数据库表中相同id的记录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String studentToUpdate_jsonStr = JSONUtil.getJSON(request);
        //将request body解析为JSONObject对象
        JSONObject req_jsonObj = JSON.parseObject(studentToUpdate_jsonStr);
        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        try {
            int studentClass_id = Integer.parseInt(req_jsonObj.get("studentClass_id").toString());
            StudentClass studentClass = StudentClassService.getInstance().find(studentClass_id);
            //将JSON字串解析为要修改的Teacher对象（有id的完整对象）
            Student studentToUpdate = JSON.parseObject(studentToUpdate_jsonStr, Student.class);
            studentToUpdate.setStudentClass(studentClass);
            //到数据库表修改Teacher对象对应的记录
            StudentService.getInstance().update(studentToUpdate);
            respMessage_jsonObj.put("message", "修改成功");
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        }
        //响应respMessage_jsonObj到前端
        response.getWriter().println(respMessage_jsonObj);
    }
}
